﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Axis.Dao.Product;
using Axis.BusinessObject.Axis.BusinessObject;
using Axis.BusinessObject.Product;
namespace Sotipha.Web.Views.Home
{
    public class CProductUI : CPage
    {
        public string GetProductHTML(int ProductTypeID, int GroupID)
        {
            StringBuilder Result = new StringBuilder();
            CProductDao coreProduct = new CProductDao();
            IList<CProduct> listProduct = coreProduct.GetProductByCondition(string.Format("<InputValue Context=\"GroupProduct\" ProductTypeID=\"{0}\" GroupID=\"{1}\"/>", ProductTypeID, GroupID));
            string Language = Session["lang"].ToString();
            string Parttern = " <li {7}><a style=\"float: left\" href=\"{0}\">" +
                                "<img src=\"{1}\" /></a>" +
                                "<div style=\"float: right; width: 45%; font-size: 12px;height: 54%;\">" +
                                    "<a class=\"tt\" href=\"{0}\">{2}</a>" +
                                    "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\" style=\"padding-top: 10px;padding-bottom: 8px;\">" +
                                     "   <tr>" +
                                      "      <td>" +
                                       "         {3}:" +
                                        "    </td>" +
                                        " <td class=\"PriceView\">" +
                                          "      <b>{4} VND</b>" +
                                           " </td>" +
                                        "</tr>" +
                                        "<tr>" +
                                         "   <td>" +
                                          "  </td>" +
                                           " <td class=\"PriceView\">" +
                                            "    <del>{5} VND</del>" +
                                            "</td>" +
                                       " </tr>" +
                                    "</table>" +
                                    "<a class=\"tt\" style=\"color: #336600; font-weight: normal;text-align: right\" href=\"{0}\">{6}</a>" +
                                "</div>" +
                            "</li>";

            if (listProduct != null)
                if (listProduct.Count > 0)
                {
                    string BlogItem = "";
                    for (int i = 0; i < listProduct.Count; i++)
                    {
                        var liRight = "";
                        if (i % 2 != 0)
                            liRight = " class='liRight' ";
                        var Item = listProduct[i];
                        var Link = BuildLink(Item);
                        string Name = Language == "vi" ? Item.Name_VN : Item.Name_EN;
                        string ltrPrice = Language != "vi" ? "Price" : "Giá";
                        string ltrAddtobag = Language != "vi" ? "Add to bag" : "Mua hàng";
                        BlogItem += string.Format(Parttern, Link, Item.Thumbnail, Name, ltrPrice, String.Format("{0:0,0}", Item.PriceOld), String.Format("{0:0,0}", Item.Price), ltrAddtobag, liRight);
                        if ((listProduct.Count == 1 || i > 0) && (i == listProduct.Count - 1 || i % 6 == 5))
                        {
                            Result.AppendFormat(" <li class=\"item\">  <ul>{0} </ul>  </li>", BlogItem);
                            BlogItem = "";
                        }
                    }
                }
            return Result.ToString();
        }
        public string GetProductHTMLTowntalk(int ProductTypeID, int GroupID)
        {
            StringBuilder Result = new StringBuilder();
            CProductDao coreProduct = new CProductDao();
            IList<CProduct> listProduct = coreProduct.GetProductByCondition(string.Format("<InputValue Context=\"GroupProduct\" ProductTypeID=\"{0}\" GroupID=\"{1}\"/>", ProductTypeID, GroupID));
            string Language = Session["lang"].ToString();
            string Parttern = " <li {7}><a style=\"float: left\" href=\"{0}\">" +
                                "<img src=\"{1}\" /></a>" +
                                "<div style=\"float: right; width: 45%; font-size: 12px;height: 54%;\">" +
                                    "<a class=\"tt\" href=\"{0}\">{2}</a>" +
                                    "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\" style=\"padding-top: 10px;padding-bottom: 8px;\">" +
                                     "   <tr>" +
                                      "      <td>" +
                                       "         {3}:" +
                                        "    </td>" +
                                        " <td class=\"PriceView\">" +
                                          "      <b>{4} VND</b>" +
                                           " </td>" +
                                        "</tr>" +
                                        "<tr>" +
                                         "   <td>" +
                                          "  </td>" +
                                           " <td class=\"PriceView\">" +
                                            "    <del>{5} VND</del>" +
                                            "</td>" +
                                       " </tr>" +
                                    "</table>" +
                                    "<a class=\"tt\" style=\"color: #336600; font-weight: normal;text-align: right\" href=\"{0}\">{6}</a>" +
                                "</div>" +
                            "</li>";

            if (listProduct != null)
                if (listProduct.Count > 0)
                {
                    string BlogItem = "";
                    for (int i = 0; i < listProduct.Count; i++)
                    {
                        var liRight = "";
                        if (i % 2 != 0)
                            liRight = " class='liRight' ";
                        var Item = listProduct[i];
                        var Link = BuildLink(Item);
                        string Name = Language == "vi" ? Item.Name_VN : Item.Name_EN;
                        string ltrPrice = Language != "vi" ? "Price" : "Giá";
                        string ltrAddtobag = Language != "vi" ? "Add to bag" : "Mua hàng";
                        double PriceOld = 0;
                        if (Item.PriceOld != null)
                            PriceOld =double.Parse(Item.PriceOld.ToString());
                        BlogItem += string.Format(Parttern, Link, Item.Thumbnail, Name, ltrPrice, String.Format("{0:0,0}", Item.PriceOld), String.Format("{0:0,0}", Item.Price), ltrAddtobag, liRight);
                        if ((listProduct.Count == 1 || i > 0) && (i == listProduct.Count - 1 || i % 4 == 3))
                        {
                            Result.AppendFormat(" <li class=\"item\">  <ul>{0} </ul>  </li>", BlogItem);
                            BlogItem = "";
                        }
                    }
                }
            return Result.ToString();
        }
        public string BuildLink(CProduct Item)
        {
            string Result = "";
            switch (Item.ProductTypeID)
            {
                case 1:
                    Result = "../Towntalk/ProductDetail.aspx?ID=" + Item.ID;
                    break;
                case 2:
                    Result = "../Lacquerware/ProductDetail.aspx?ID=" + Item.ID;
                    break;
                case 3:
                    Result = "../Gifts/ProductDetail.aspx?ID=" + Item.ID;
                    break;
                case 4:
                    Result = "../Sale/ProductDetail.aspx?ID=" + Item.ID;
                    break;
            }
            return Result;
        }
        public string GetProductHTMLForSale(int ProductTypeID)
        {
            StringBuilder Result = new StringBuilder();
            CProductDao coreProduct = new CProductDao();
            IList<CProduct> listProduct = coreProduct.GetProductByCondition(string.Format("<InputValue Context=\"ProductType\" ProductTypeID=\"{0}\" />", ProductTypeID));
            string Language = Session["lang"].ToString();
            string Parttern = " <li {6}><a href=\"{0}\">" +
                                "<img src=\"{1}\" /></a>" +
                                "<div>" +
                                 "   <a class=\"tt\" href=\"{0}\">{2}</a>" +
                                  "  <table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\" style=\"padding-top: 20px\">" +
                                   "     <tr>" +
                                    " <td>{3}: </td>" +
                                            "<td>" +
                                                "<del>{4}</del>" +
                                            "</td>" +
                                            "<td>" +
                                                "<b>{5}</b>" +
                                            "</td>" +
                                        "</tr>" +
                                    "</table>" +
                                "</div>" +
                            "</li>";
            string Pattern2 = "<li class=\"item\"> <ul>{0}</ul></li>";
            string liItem = "";
            if (listProduct != null)
                if (listProduct.Count > 0)
                {
                    for (int i = 0; i < listProduct.Count; i++)
                    {
                        var Item = listProduct[i];
                        if (i > 0 && i % 5 == 4)
                        {
                            Result.AppendFormat(Pattern2, liItem);
                            liItem = "";
                        }
                        string style = "";
                        if (i % 4 == 3)
                            style = " style=\"padding-right: 0px\"";
                        var Link = BuildLink(Item);
                        string Name = Language == "vi" ? Item.Name_VN : Item.Name_EN;
                        string ltrPrice = Language != "vi" ? "Price" : "Giá";
                        string ltrAddtobag = Language != "vi" ? "Add to bag" : "Mua hàng";
                        liItem += string.Format(Parttern, Link, Item.Thumbnail, Name, ltrPrice, String.Format("{0:0,0}", Item.PriceOld), String.Format("{0:0,0}", Item.Price), style);
                    }
                    if (liItem.Length > 0)
                        Result.AppendFormat(Pattern2, liItem);
                }
            return Result.ToString();
        }
    }
}